package com.jp.knowledge.my.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.jp.knowledge.R;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.base.RecycleVHolder;
import com.jp.knowledge.my.business.Vector;
import com.jp.knowledge.my.model.ContactsModel;
import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersAdapter;

import org.xutils.x;

import java.util.List;

/**
 * @author gzejia 978862664@qq.com
 */
public class ContactsAdapter extends RecycleVAdapter<ContactsModel> implements
        StickyRecyclerHeadersAdapter<RecycleVHolder> {

    public ContactsAdapter(Context context, @NonNull List<ContactsModel> list) {
        super(context, list);
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.item_contacts;
    }

    @Override
    public void onBind(RecycleVHolder holder, int position) {
        ContactsModel model = getData(position);
        holder.setTextView(R.id.item_contacts_tv_name, model.getName());
        holder.setTextView(R.id.item_contacts_tv_branch, !TextUtils.isEmpty(model.getBranch()) ?
                model.getBranch() : model.getPhone());
        x.image().bind(holder.getImageView(R.id.item_contacts_iv_header), model.getUrl(),
                Vector.IMAGE_GENERAL);

        Button inviteBtn = holder.getButton(R.id.item_contacts_btn_invite);
        if (TextUtils.isEmpty(model.getStats()) || model.getStats().equals("1")) {
            inviteBtn.setText("邀请");
            inviteBtn.setBackgroundResource(R.drawable.my_rbg_blue_press);
            inviteBtn.setTextColor(mContext.getResources().getColor(R.color.white));
        } else if (model.getStats().equals("0")) {
            inviteBtn.setText("已添加");
            inviteBtn.setBackgroundResource(R.color.translate);
            inviteBtn.setTextColor(mContext.getResources().getColor(R.color.gray_light));
        }
    }

    @Override
    public long getHeaderId(int position) {
        return getData(position).getRemark().charAt(0);
    }

    @Override
    public RecycleVHolder onCreateHeaderViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_remark_header, parent, false);
        return new RecycleVHolder(view) {
        };
    }

    @Override
    public void onBindHeaderViewHolder(RecycleVHolder holder, int position) {
        String remarkStr = getData(position).getRemark();
        holder.setTextView(R.id.remark_header_tv, remarkStr);
    }
}
